/**
 * Created by TOPFEEL on 2018/2/24.
 */

layui.use(['layer', 'table', 'form'], function () {
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;

    //表单重新渲染
    form.render();

    //表格渲染
    var tableObj = table.render({
        id: 'list_table'
        , elem: '#list_table'
        , url: 'sys/res/list'
        , align: "center"
        , cols: [[ //表头
            {type: 'numbers', title: "序号", width: 50}
            , {field: 'resName', title: '资源名称', width: 150, align: 'center'}
            , {
                field: 'resType', title: '资源类型', width: 90, templet: function (d) {
                    return d.resType == 1 ? '菜单' : '功能'
                }, unresize: true, align: 'center'
            }
            , {field: 'resCode', title: '资源编码', width: 150, align: 'center'}
            , {field: 'resUrl', title: '菜单URL', width: 220, align: 'center'}
            // , {field: 'resIcon', title: '菜单图标', width: 100, align: 'center'}
            , {field: 'resOrder', title: '排序', width: 70, align: 'center'}
            , {
                field: 'isLeaf', title: '叶子节点', width: 90, templet: function (d) {
                    return d.isLeaf == 0 ? '否' : '是'
                }, align: 'center'
            }
            , {
                field: 'resState', title: '状态', width: 70, templet: function (d) {
                    return d.resState == 0 ? '<span class="font-danger">禁用</span>' : '<span class="font-success">启用</span>'
                }, align: 'center'
            }
            , {field: 'createTime', title: '添加时间', width: 170, align: 'center'}
            , {
                field: 'lock', title: '操作', width: 150, templet: function (d) {
                    return d.isSystem == '1' ? '<button class="layui-btn layui-btn-sm layui-btn-disabled" disabled="disabled">修改</button>' +
                    '<button class="layui-btn layui-btn-sm layui-btn-disabled" disabled="disabled">删除</button>' :
                    '<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="edit">修改</button>' +
                    '<button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="delete">删除</button>';
                }, unresize: true, align: 'center'
            }
        ]], where: {"parentId": "0"}
    });

    //监听工具条
    table.on('tool(mainList)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data //获得当前行数据
            , layEvent = obj.event; //获得 lay-event 对应的值

        if (layEvent === 'edit') {
            showDialog({
                title: '修改'
                , template: 'res-edit'
                , saveUrl: 'sys/res/save'
                , tableObj: tableObj
                , width: 500
                , height: 600
                , htmlData: data
                , submited: function () {
                    getZtree();
                }
            })
        }
        if (layEvent === 'delete') {
            var obj = {"resId": data.resId};
            showConfirm('确认删除？', function () {
                $.ajax({
                    type: "GET",
                    url: 'sys/res/delete',
                    data: obj,
                    success: function (result) {
                        layer.msg(result.msg);
                        if (result.success) {
                            reloadTable(tableObj);
                            getZtree();
                        }
                    }
                })
            }, 0);
        }
    });


    // 顶部 [添加]和[删除]操作事件
    $("#res_add").click(function () {
        var nodes = ztreeObj.getSelectedNodes();
        if (nodes.length == 0) {
            layer.msg("请选择节点");
            return;
        }
        var node = nodes[0];

        showDialog({
            title: '添加'
            , template: 'res-add'
            , saveUrl: 'sys/res/save'
            , tableObj: tableObj
            , width: 600
            , height: 600
            , htmlData: node
            , afterSubmit: function () {
                getZtree();
            }
        });
    });

    //修改
    $("#res_edit").click(function () {
        var nodes = ztreeObj.getSelectedNodes();
        if (nodes.length == 0) {
            layer.msg("请选择节点");
            return;
        }
        var node = nodes[0];
        if (node.level == 0) {
            layer.msg("根节点不能修改");
            return;
        }
        if (node.attrs.sysFlag == '1') {
            layer.msg("系统数据禁止修改");
            return;
        }
        $.ajax({
            url: 'sys/res/getres?resId=' + node.id,
            contentType: 'application/json',
            success: function (result) {
                if (result && result.data) {
                    showDialog({
                        title: '修改'
                        , template: 'edit'
                        , saveUrl: '/sys/res/save'
                        , tableObj: tableObj
                        , width: 600
                        , height: 600
                        , htmlData: result.data
                        , afterSubmit: function () {
                            getZtree();
                        }
                    });
                }
            },
            error: function () {
                layer.msg("操作异常");
            }
        });


    });

    $("#res_delete").click(function () {
        var nodes = ztreeObj.getSelectedNodes();
        console.log(nodes);
        if (nodes.length == 0) {
            layer.msg("请选择节点");
            return;
        }
        var node = nodes[0];
        if (node.level == 0) {
            layer.msg("根节点不能删除");
            return;
        }
        if (node.attrs.sysFlag == '1') {
            layer.msg("系统数据禁止删除");
            return;
        }


        var obj = {"resId": node.id};
        showConfirm('确认删除？', function () {
            $.ajax({
                type: "GET",
                url: 'sys/res/delete',
                data: obj,
                success: function (result) {
                    layer.msg(result.msg);
                    if (result.success) {
                        reloadTable(tableObj);
                        getZtree();
                    }
                }
            })
        }, 0);
    });

    //查询数据
    $('#query').on('click', function () {
        reloadTable(tableObj);
    });


    var ztreeObj = null;

    var setting = {
        data: {
            key: {
                title: "t"
            },
            simpleData: {
                enable: true
            }
        },
        view: {
            showIcon: true,
            showTitle: false
        },
        callback: {
            onClick: ztreeCallback
        }
    };


    //获取左侧ztree
    getZtree();

    function getZtree() {
        $.ajax({
            url: 'sys/res/restree',
            contentType: 'application/json',
            success: function (result) {
                if (result && result.data) {
                    ztreeObj=initZtree("ztree", setting, result.data);
                }else{
                  layer.msg("数据异常");
                }
            }
        });
    }

    function ztreeCallback(event, treeId, treeNode) {
        var parentFlag = treeNode.isParent;
        var nodeId = treeNode.id;
        if (parentFlag) {
            $("#list_form input[name='parentId']").val(nodeId);
            $("#list_form input[name='resId']").val("");
        } else {
            $("#list_form input[name='resId']").val(nodeId);
            $("#list_form input[name='parentId']").val("");
        }
        reloadTable(tableObj);
    }

});
